<?php
/**
 * Created by PhpStorm.
 * User: ysongyang
 * Date: 2016/6/7
 * Time: 12:19
 */
namespace Admin\Controller;

class ArticleController extends CommonController{

    function _initialize()
    {
        parent::_initialize();
        $this->_article = D('Article');
    }
    // 文章首页
    public function index()
    {
        $where = array('art_del_stats' => 0); // 定义where 读取没有被删除的文章
        if(IS_POST){
            $where['cate_id']=array('like',$_POST['cate_id']);
            $where['art_title']=array('like','%'.$_POST['keywords'].'%');
        }

        $count = $this->_article->where($where)->count();
        $page = new \Think\Page($count, 10); // 实例化分页类 条数，分页数
        $limit = $page->firstRow . ',' . $page->listRows;
        // relation('cate') 定义关联模型的某个表
        $this->cate = D('Category')->getCategoryTree();
        $this->article = $this->_article->where($where)->relation(true)->limit($limit)->select();
        $this->page = $page->show();
        $this->display();
    }


    public function trach()
    {
        $where = array('art_del_stats' => 1); // 定义where 读取没有被删除的文章
        if(IS_POST){
            $where['cate_id']=array('like',$_POST['cate_id']);
            $where['art_title']=array('like','%'.$_POST['keywords'].'%');
        }
        $count = $this->_article->where($where)->count();
        $page = new \Think\Page($count, 10); // 实例化分页类 条数，分页数
        $limit = $page->firstRow . ',' . $page->listRows;
        // relation('cate') 定义关联模型的某个表
        $this->cate = D('Category')->getCategoryTree();
        $this->article = $this->_article->where($where)->relation(true)->limit($limit)->select();
        $this->page = $page->show();
        $this->display();
    }

    // 添加文章
    public function add(){
        if(IS_POST){
            $_POST['art_time']=time();
            // 验证通过 写入新增数据
            $res = $this->_article->add($_POST);
            if($res){
                    $this->success('添加文章成功！', U(MODULE_NAME.'/Article/index'));
            }else{
                    $this->error('添加文章失败！');
            }

        }else{
            $this->cate = D('Category')->getCategoryTree();
            $this->display();
        }
    }

    public function edit(){
        if(IS_POST){
            $art_id = $_GET['art_id'];
            $data =array(
                'cate_id'=>$_POST['cate_id'],
                'art_title'=>$_POST['art_title'],
                'art_editor'=>$_POST['art_editor'],
                'art_thumb'=>$_POST['art_thumb'],
                'art_tag'=>$_POST['art_tag'],
                'art_description'=>$_POST['art_description'],
                'art_summary'=>$_POST['art_summary'],
                'art_content'=>$_POST['art_content'],
                'art_time'=>time()
            );
            if ($this->_article->where(array('art_id' => $art_id)) -> save($data)) {
                $this -> success('更新文章成功！', U(MODULE_NAME . '/Article/index'));
            }else{
                $this->error('更新文章失败！');
            }

        }else{
            $art_id = I('art_id');
            $this->cate = D('Category')->getCategoryTree();
            $this->field = $this->_article->find($art_id);//查找当前文章ID数据
            $this->display();
        }
    }

    //删除文章
    public function destroy($art_id){
        $res = $this->_article->where(array('art_id'=>$art_id))->delete();
        if($res){
            $data =[
                'status'=>1,
                'info'=>'删除文章成功！',
            ];
        }else{
            $data = [
                'status'=>0,
                'info'=>'删除文章失败，请稍后再试！',
            ];
        }
        $this->ajaxReturn($data);
    }

    //删除到回收站方法 及 回收站列表
    public function recycle()
    {
        if(IS_POST){
            $art_id = I('art_id',0,'intval');
            $stats = I('stats');
            $res = $this->_article->where(array('art_id'=>$art_id))->save(array('art_del_stats'=>$stats));
            if($res){
                $data =[
                    'status'=>1,
                    'info'=>'删除到回收站成功！',
                ];
            }else{
                $data = [
                    'status'=>0,
                    'info'=>'删除失败，请稍后再试！',
                ];
            }
            $this->ajaxReturn($data);
        }
    }

    //恢复文章
    public function recovery()
    {
        if(IS_POST){
            $art_id = I('art_id',0,'intval');
            $stats = I('stats');
            $res = $this->_article->where(array('art_id'=>$art_id))->save(array('art_del_stats'=>$stats));
            if($res){
                $data =[
                    'status'=>1,
                    'info'=>'恢复文章成功！',
                ];
            }else{
                $data = [
                    'status'=>0,
                    'info'=>'恢复失败，请稍后再试！',
                ];
            }
            $this->ajaxReturn($data);
        }
    }
    
    
    //批量删除文章
    public function deleteAll(){
        if(IS_POST){
            $artids = $_POST['art_id'];
            $res = $this->_article->where(array('art_id'=>array('in',$artids)))->delete();
            if($res){
                $data =[
                    'status'=>1,
                    'info'=>'批量删除文章成功！',
                ];
            }else{
                $data = [
                    'status'=>0,
                    'info'=>'批量删除文章失败，请稍后再试！',
                ];
            }
            $this->ajaxReturn($data);
        }
    }
}